package com.cat.graphTheory;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/count-unreachable-pairs-of-nodes-in-an-undirected-graph/description/
 * @create 2025/9/18 20:20
 * @since JDK17
 */

public class Solution04 {
    boolean[] vis;
    List<List<Integer>> g;
    long dfs(int cur) {
        vis[cur] = true;
        long size = 1;
        for (int x : g.get(cur)) {
            if (!vis[x]) {
                size += dfs(x);
            }
        }
        return size;
    }

    public long countPairs(int n, int[][] edges) {
        long ans = 0;
        int m = 0;
        g = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            g.add(new ArrayList<>());
        }
        for (int i = 0; i < m; i++) {   // 建图
            int from = edges[i][0], to = edges[i][1];
            g.get(from).add(to);
        }

        vis = new boolean[n];
        for (int i = 0; i < n; i++) {
            if (!vis[i]) {
                long size = dfs(i);
                ans += size * (n - size);
            }
        }

        return ans / 2;
    }
}
